package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.FriendDto;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.GroupMemberDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.chat.ChatMessageDto;
import com.lanjiu.lib.imclient.dto.chat.TextDto;
import com.lanjiu.lib.imclient.dto.extra.ChatFriendMsgDto;
import com.lanjiu.lib.imclient.dto.extra.ChatGroupMsgDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.GUID;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/22 20:17
 */
public class AddFriendView extends IView {

    private AddFriendCallback mCallback;

    public AddFriendView(AddFriendCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doApplyFriend(String friendUserId, String friendNick, String friendAvatar, String friendUserType, String levelMessage) {
        ChatFriendMsgDto chatFriendMsgDto = new ChatFriendMsgDto();
        FriendDto friendDto = new FriendDto(UserPreferences.isRegistered() ? IMSContacts.UserType.REGISTERED_USER : IMSContacts.UserType.TOURISTS_USER);
        friendDto.setFriendUserId(friendUserId);
        friendDto.setFriendNickname(friendNick);
        friendDto.setFriendAvatar(friendAvatar);
        friendDto.setUserType(friendUserType);
        chatFriendMsgDto.setFriendDto(friendDto);
        ChatMessageDto chatMessageDto = new ChatMessageDto();
        chatMessageDto.setChatType(UserPreferences.isRegistered() ? IMSContacts.MsgContentType.APPLY_FRIEND_REGISTERED : IMSContacts.MsgContentType.APPLY_FRIEND_TOURISTS);
        chatMessageDto.setMsgId(GUID.random());
        TextDto textDto = new TextDto();
        textDto.setContent(levelMessage);
        textDto.setLength(String.valueOf(levelMessage.length()));
        chatMessageDto.setText(textDto);
        chatFriendMsgDto.setChatMessageDto(chatMessageDto);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_APPLY_REGISTERED : EventConstants.Action.FRIEND_APPLY_TOURISTS;
        doSendRequest(action, chatFriendMsgDto.toJson());
    }

    public void doApplyGroup(String groupId, String groupName, String groupOwnerId, String levelMessage) {
        ChatGroupMsgDto chatGroupMsgDto = new ChatGroupMsgDto();
        GroupMemberDto groupMemberDto = new GroupMemberDto(UserPreferences.getUserType());
        groupMemberDto.setGroupMemberUserId(UserPreferences.getUserId());
        groupMemberDto.setGroupNemberName(UserPreferences.getNickname());
        groupMemberDto.setGroupMemberAvatar(UserPreferences.getAvatar());
        GroupDto groupDto = new GroupDto(UserPreferences.getUserType());
        groupDto.setGroupId(groupId);
        groupDto.setGroupName(groupName);
        groupDto.setGroupOwnerUserId(groupOwnerId);
        groupDto.setGroupType(IMSUtils.userTypeAnalysis(groupId));
        groupDto.setSnapchatStatus(false);
        groupMemberDto.setGroup(groupDto);
        chatGroupMsgDto.setGroupMemberDto(groupMemberDto);
        ChatMessageDto chatMessageDto = new ChatMessageDto();
        chatMessageDto.setChatType(UserPreferences.isRegistered() ? IMSContacts.MsgContentType.APPLY_REGISTERED : IMSContacts.MsgContentType.APPLY_TOURISTS);
        chatMessageDto.setMsgId(GUID.random());
        TextDto textDto = new TextDto();
        textDto.setContent(levelMessage);
        textDto.setLength(String.valueOf(levelMessage.length()));
        chatMessageDto.setText(textDto);
        chatGroupMsgDto.setChatMessageDto(chatMessageDto);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_MEMBER_APPLY_REGISTERED : EventConstants.Action.GROUP_MEMBER_APPLY_TOURISTS;
        doSendRequest(action, chatGroupMsgDto.toJson());
    }

    @Override
    public void onMessageEventHandle(MessageEvent event) {
        Logger.print("AddFriendView", event.toString());
        if (event.action() == EventConstants.Action.FRIEND_APPLY_TOURISTS
                || event.action() == EventConstants.Action.FRIEND_APPLY_REGISTERED) {
            if (null != mCallback)
                mCallback.onAddFriendCallback(event.code(), event.msg());
        } else if (event.action() == EventConstants.Action.GROUP_MEMBER_APPLY_TOURISTS
                || event.action() == EventConstants.Action.GROUP_MEMBER_APPLY_REGISTERED) {
            GroupMemberDto groupMemberDto = null;
            if (checkIsSuccess(event.code(), event.data())) {
                groupMemberDto = Dto.fromJson(event.data(), GroupMemberDto.class);
            }
            if (null != mCallback)
                mCallback.onAddGroupCallback(event.code(), event.msg(), groupMemberDto);
        }
    }

    public interface AddFriendCallback {

        void onAddFriendCallback(int code, String msg);

        void onAddGroupCallback(int code, String msg, GroupMemberDto data);
    }
}
